home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue54 / Persist / tiPtnVisitorMgr.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-01-21  |  5.3 KB  |  176 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   (c) TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: Jan 2000
  10.  
  11.   Notes: Manage a family of visitors. Based on the factory pattern.
  12.  
  13. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  14. unit tiPtnVisitorMgr ;
  15.  
  16. interface
  17. uses
  18.   Classes
  19.   ,tiPtnVisitor
  20.   ,tiDBConnection
  21.   ,tiPtnVisitorDB
  22.   ;
  23.  
  24. type
  25.  
  26.   // TVisClassRef: A class reference type for storing visitor info
  27.   //----------------------------------------------------------------------------
  28.   TVisClassRef = class of TVisitorAbs ;
  29.  
  30.   // TVisMapping: There will be one of these for each visitor which has
  31.   //              been registered with the manager.
  32.   //----------------------------------------------------------------------------
  33.   TVisMapping = class( TObject )
  34.   private
  35.     FsGroupName : string ;
  36.     FClassRef   : TVisClassRef ;
  37.     FVisitor    : TVisitorAbs ;
  38.     FDBConnection: TtiDBConnection;
  39.   public
  40.     constructor CreateExt( const psGroupName : string ;
  41.                            const pClassRef : TVisClassRef ) ;
  42.     property    GroupName : string read FsGroupName write FsGroupName ;
  43.     property    ClassRef  : TVisClassRef read FClassRef write FClassRef ;
  44.     property    Visitor   : TVisitorAbs read FVisitor write FVisitor ;
  45.     property    DBConnection : TtiDBConnection read FDBConnection write FDBConnection ;
  46.     procedure   Execute( pVisited : TVisitedAbs ) ;
  47.   end ;
  48.  
  49.   // TVisitorCache: Manage the cached list of visitors.
  50.   //----------------------------------------------------------------------------
  51.   TVisitorCache = class( TObject )
  52.   private
  53.     FVisMappings : TStringList ;
  54.     FDBConnection : TtiDBConnection ;
  55.   public
  56.     constructor create ;
  57.     destructor  destroy ; override ;
  58.     // Register a new visitor with the manager.
  59.     procedure   RegisterVisitor( const psGroupName : string ;
  60.                                  const pClassRef : TVisClassRef ) ;
  61.     // Pass a TVisitedAbs, and execute a family of visitors as identified by
  62.     // psGroupName
  63.     procedure   Execute( const psGroupName : string ;
  64.                          pVisited : TVisitedAbs ) ;
  65.   end ;
  66.  
  67. function gVisitorCache  : TVisitorCache ;
  68.  
  69. implementation
  70. uses
  71.   SysUtils
  72.   ,Windows
  73.   ,tiUtils
  74.   ;
  75.  
  76. var
  77.   uVisitorCache   : TVisitorCache ;
  78.  
  79. //------------------------------------------------------------------------------
  80. function gVisitorCache   : TVisitorCache ;
  81. begin
  82.   if uVisitorCache = nil then
  83.     uVisitorCache := TVisitorCache.Create ;
  84.   result := uVisitorCache ;
  85. end ;
  86.  
  87. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  88. // *
  89. // * TVisMgrCache
  90. // *
  91. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  92. constructor TVisitorCache.create;
  93. begin
  94.   inherited ;
  95.   FVisMappings := TStringList.Create ;
  96.   FDBConnection := TtiDBConnection.Create ;
  97. end;
  98.  
  99. //------------------------------------------------------------------------------
  100. destructor TVisitorCache.destroy;
  101. var
  102.   i : integer ;
  103. begin
  104.   inherited;
  105.  
  106.   for i := 0 to FVisMappings.Count - 1 do
  107.     TObject( FVisMappings.Objects[i] ).Free ;
  108.  
  109.   FVisMappings.Free ;
  110.   FDBConnection.Free ;
  111.  
  112. end ;
  113.  
  114. //------------------------------------------------------------------------------
  115. procedure TVisitorCache.Execute(const psGroupName: string;pVisited : TVisitedAbs);
  116. var
  117.   i : integer ;
  118.   lsGroupName : string ;
  119. begin
  120.   lsGroupName := upperCase( psGroupName ) ;
  121.   for i := 0 to FVisMappings.Count - 1 do
  122.     if FVisMappings.Strings[i] = lsGroupName then begin
  123.       TVisMapping( FVisMappings.Objects[i] ).DBConnection := FDBConnection ;
  124.       TVisMapping( FVisMappings.Objects[i] ).Execute( pVisited ) ;
  125.     end ;
  126. end;
  127.  
  128. //------------------------------------------------------------------------------
  129. procedure TVisitorCache.RegisterVisitor( const psGroupName : string ;
  130.                                         const pClassRef : TVisClassRef ) ;
  131. var
  132.   lVisMapping : TVisMapping ;
  133. begin
  134.   lVisMapping := TVisMapping.CreateExt( psGroupName, pClassRef ) ;
  135.   FVisMappings.AddObject( lVisMapping.GroupName, lVisMapping ) ;
  136. end;
  137.  
  138. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  139. // *
  140. // * TVisMapping
  141. // *
  142. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  143. constructor TVisMapping.CreateExt(const psGroupName: string;
  144.   const pClassRef: TVisClassRef);
  145. begin
  146.   Create ;
  147.   GroupName := upperCase( psGroupName ) ;
  148.   ClassRef  := pClassRef ;
  149.   Visitor   := nil ;
  150. end;
  151.  
  152. //------------------------------------------------------------------------------
  153. procedure TVisMapping.Execute( pVisited : TVisitedAbs ) ;
  154. begin
  155.   Assert( pVisited <> nil, 'Visited not asigned' ) ;
  156.   if Visitor = nil then
  157.     Visitor := ClassRef.Create ;
  158.   if Visitor is TVisDBAbs then
  159.     TVisDBAbs( Visitor ).DBConnection := DBConnection ;
  160.   pVisited.Iterate( Visitor ) ;
  161.   if Visitor is TVisDBAbs then
  162.     TVisDBAbs( Visitor ).DBConnection := nil ;
  163. end;
  164.  
  165. initialization
  166.  
  167.   gVisitorCache ;
  168.  
  169. finalization
  170.   uVisitorCache.Free ;
  171.  
  172. end.
  173.  
  174.  
  175.  
  176.